#include "expframe.h"
#include "ui_expframe.h"

#include "sourcemanage.h"

ExpFrame::ExpFrame(QWidget *parent,Experiment *pExperiment) :
    QFrame(parent),
    ui(new Ui::ExpFrame)
{
    ui->setupUi(this);
    if(pExperiment==NULL)
    {
        pExperiment=new Experiment;
    }
    this->pExperiment=pExperiment;
    ExpMethod *pExpMethod =pExperiment->GetExpMethodPtr();
    AnalyteDataBase *pAnalyteDataBase=pExperiment->GetAnalyteDataBase();
    pExpMethod->InitMethod(pAnalyteDataBase);
    pExperiment->Analyzed();

    //pExpAnalysisElementFrame=new ExpAnalysisElementFrame(this);//
    pSpectralLineFrame=new SpectralLineFrame(this,pAnalyteDataBase,1);
    pExpInstrumentCalFrame=new ExpInstrumentCalFrame(this,pExperiment);
    pExpInstrumentParaFrame=new ExpInstrumentParaFrame(this,pExperiment);
    pExpCollectionParaFrame=new ExpCollectionParaFrame(this,pExperiment);
    pExpExtractTimeFrame=new ExpExtractTimeFrame(this,pExperiment);
    pExpInternalStandardFrame=new ExpInternalStandardFrame(this,pExperiment);
    //pExpIsotopeRatioFrame=new ExpIsotopeRatioFrame(this);//
    pATuneMonitorRatioFrame=new ATuneMonitorRatioFrame(this,pExperiment);
    pExpQcFrame=new ExpQcFrame(this);
    pExpQuantitativeMethodFrame=new ExpQuantitativeMethodFrame(this,pExperiment);
    pExpReportFrame=new ExpReportFrame(this);
    pExpResultFrame=new ExpResultFrame(this,pExperiment);
    pExpSampleListFrame=new ExpSampleListFrame(this,pExperiment);

    //ui->tabWidget->addTab(pExpAnalysisElementFrame,tr("分析元素"));
    ui->tabWidget->addTab(pSpectralLineFrame,tr("分析元素"));
    ui->tabWidget->addTab(pExpInstrumentCalFrame,tr("仪器校正"));
    ui->tabWidget->addTab(pExpInstrumentParaFrame,tr("仪器参数"));
    ui->tabWidget->addTab(pExpCollectionParaFrame,tr("采集参数"));
    ui->tabWidget->addTab(pExpExtractTimeFrame,tr("提取时间"));
    ui->tabWidget->addTab(pExpInternalStandardFrame,tr("内标"));
    //ui->tabWidget->addTab(pExpIsotopeRatioFrame,tr("同位素比"));
    ui->tabWidget->addTab(pATuneMonitorRatioFrame,tr("监控比值"));
    ui->tabWidget->addTab(pExpQuantitativeMethodFrame,tr("定量方法"));
    ui->tabWidget->addTab(pExpQcFrame,tr("QC"));
    ui->tabWidget->addTab(pExpSampleListFrame,tr("样本列表"));
    ui->tabWidget->addTab(pExpResultFrame,tr("结果"));
    ui->tabWidget->addTab(pExpReportFrame,tr("报表"));

    connect(pSpectralLineFrame,SIGNAL(SIG_ElementChooseChanged(bool,QString)),
            pExpCollectionParaFrame,SLOT(OnElementChooseChanged(bool,QString)));
}

ExpFrame::~ExpFrame()
{
    qDebug()<<"ExpFrame::~ExpFrame()";
    if(pExperiment!=NULL)
    {
        delete pExperiment;
    }
    delete ui;
}

void ExpFrame::LoadData()
{

}

int ExpFrame::SaveData()
{
    QString filePath=pExperiment->GetFilePath();
    QFileInfo fileInfo(filePath);
    if(fileInfo.fileName().contains(FM->GetExpsSuffix())==false)
    {
        filePath=QFileDialog::getSaveFileName(this, tr("Save File"),FM->GetExpsDir(),
                                              tr("Bin files (*%1)").arg(FM->GetExpsSuffix()));
        if(filePath=="") return 0;
        pExperiment->SetFilePath(filePath);
    }
    else//
    {

    }
    int state=FM->SaveToFile(pExperiment->GetFilePath(),pExperiment);
    qDebug()<<"pExperiment save"<<state; 

    fileInfo.setFile(filePath);
    emit SIG_ExpSave(fileInfo.fileName());

    return 1;
}

void ExpFrame::on_tabWidget_currentChanged(int index)
{
    QWidget *pWidget=ui->tabWidget->widget(index);
    QString name=pWidget->objectName();

    qDebug()<<pWidget<<name;
    if(name=="ExpInternalStandardFrame")
    {
       pExpInternalStandardFrame->LoadData();
    }
    else if(name=="ExpQuantitativeMethodFrame")
    {
       pExpQuantitativeMethodFrame->LoadData();
    }
    else if(name=="ATuneMonitorRatioFrame")
    {
       pATuneMonitorRatioFrame->LoadData();
    }
    else if(name=="ExpSampleListFrame")//
    {
       pExpSampleListFrame->LoadData();
    }
    else if(name=="ExpCollectionParaFrame")//
    {
       pExpCollectionParaFrame->LoadData();
    }
    else if(name=="ExpResultFrame")
    {
       pExpResultFrame->LoadData();
    }
    else if(name=="SpectralLineFrame")//
    {
       pSpectralLineFrame->LoadData();
    }
    else if(name=="ExpInstrumentParaFrame")//
    {
       pExpInstrumentParaFrame->LoadData();
    }
}
